package com.ruoyi.blog.service.impl;

import java.util.List;

import com.ruoyi.blog.config.MyResourcesConfig;
import com.ruoyi.blog.service.ITBlogConfigService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.blog.mapper.TBlogMapper;
import com.ruoyi.blog.domain.TBlog;
import com.ruoyi.blog.service.ITBlogService;

/**
 * 博客Service业务层处理
 * 
 * @author yang
 * @date 2021-11-15
 */
@Service
public class TBlogServiceImpl implements ITBlogService 
{
    @Autowired
    private TBlogMapper tBlogMapper;

    @Autowired
    private MyResourcesConfig MyResourcesConfig;

    @Autowired
    private ITBlogConfigService tBlogConfigService;


    /**
     * 查询博客
     * 
     * @param blogId 博客主键
     * @return 博客
     */
    @Override
    public TBlog selectTBlogByBlogId(Long blogId)
    {
        TBlog tBlog = tBlogMapper.selectTBlogByBlogId(blogId);
//        if(tBlog.getCoverImgUrl() != null){
//            tBlog.setCoverImgUrl("http://127.0.0.1:9300/statics/" + tBlog.getCoverImgUrl());
//            tBlog.setCoverImgUrl(MyResourcesConfig.getFullPath(tBlog.getCoverImgUrl()));
//        }
        return tBlog;
    }

    /**
     * 查询博客列表
     * 
     * @param tBlog 博客
     * @return 博客
     */
    @Override
    public List<TBlog> selectTBlogList(TBlog tBlog)
    {
        List<TBlog> listBlog = tBlogMapper.selectTBlogList(tBlog);
//        for(TBlog blog:listBlog){
//            if(blog.getCoverImgUrl() != null){
//                blog.setCoverImgUrl("/profile/" + blog.getCoverImgUrl());
//            }
//        }
        return listBlog;
    }

    /**
     * 新增博客
     * 
     * @param tBlog 博客
     * @return 结果
     */
    @Override
    public int insertTBlog(TBlog tBlog)
    {
        tBlog.setCreateTime(DateUtils.getNowDate());
        tBlog.setUpdateTime(DateUtils.getNowDate());
        tBlogConfigService.addblogCounts();
        return tBlogMapper.insertTBlog(tBlog);
    }

    /**
     * 修改博客
     * 
     * @param tBlog 博客
     * @return 结果
     */
    @Override
    public int updateTBlog(TBlog tBlog)
    {
        tBlog.setUpdateTime(DateUtils.getNowDate());
        return tBlogMapper.updateTBlog(tBlog);
    }

    /**
     * 批量删除博客
     * 
     * @param blogIds 需要删除的博客主键
     * @return 结果
     */
    @Override
    public int deleteTBlogByBlogIds(Long[] blogIds)
    {
        int res = tBlogMapper.deleteTBlogByBlogIds(blogIds);
        if(res > 0){
            tBlogConfigService.setAddAddOrDeleteDelete("blogCounts",-res);
        }
        return res;
    }

    /**
     * 删除博客信息
     * 
     * @param blogId 博客主键
     * @return 结果
     */
    @Override
    public int deleteTBlogByBlogId(Long blogId)
    {
        int res = tBlogMapper.deleteTBlogByBlogId(blogId);
        if(res > 0)
            tBlogConfigService.deleteblogCounts();
        return res;
    }

    @Override
    public List<TBlog> selectListBlogFromIndex(TBlog tBlog, String orderBy) {
        List<TBlog> listBlog = tBlogMapper.selectListBlogFromIndex(tBlog,orderBy);
//        for(TBlog blog:listBlog){
//            if(blog.getCoverImgUrl() != null){
//                blog.setCoverImgUrl("http://127.0.0.1:9300/statics/" + blog.getCoverImgUrl());
//                blog.setCoverImgUrl(MyResourcesConfig.getFullPath(blog.getCoverImgUrl()));
//            }
//        }
        return listBlog;
    }


    /**
     * 增加文章次数
     *
     * @param blogId
     * @param count 可以为空，如果为空，自动加1
     * @return
     */
    @Override
    public int addLikeCount(Long blogId, Long count) {
        TBlog updateBlog = new TBlog();
        updateBlog.setBlogId(blogId);
        if(count == null){
            TBlog blog = selectTBlogByBlogId(blogId);
            updateBlog.setLikeCount(blog.getLikeCount() + 1);
        }else{
            updateBlog.setLikeCount(count);
        }
        return tBlogMapper.updateTBlog(updateBlog);
    }
}
